package com.qiaoba.module.system.converter;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qiaoba.api.system.entity.dto.SysUserDTO;
import com.qiaoba.api.system.entity.vo.SysUserVO;
import com.qiaoba.module.system.entity.SysUser;
import com.qiaoba.module.system.entity.param.SysUserParam;

/**
 * 用户 类型转换
 *
 * @author ailanyin
 * @date 2023-09-10 08:57:58
 */
public class SysUserConverter {

    public static SysUser dto2Entity(SysUserDTO dto) {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(dto.getUserId());
        sysUser.setDeptId(dto.getDeptId());
        sysUser.setUsername(dto.getUsername());
        sysUser.setNickname(dto.getNickname());
        sysUser.setEmail(dto.getEmail());
        sysUser.setPhone(dto.getPhone());
        sysUser.setGender(dto.getGender());
        sysUser.setStatus(dto.getStatus());
        sysUser.setRemark(dto.getRemark());

        return sysUser;
    }

    public static SysUserVO entity2Vo(SysUser sysUser) {
        SysUserVO sysUserVO = new SysUserVO();
        sysUserVO.setUserId(sysUser.getUserId());
        sysUserVO.setUsername(sysUser.getUsername());
        sysUserVO.setPassword(sysUser.getPassword());
        sysUserVO.setNickname(sysUser.getNickname());
        sysUserVO.setDeptId(sysUser.getDeptId());
        sysUserVO.setGender(sysUser.getGender());
        sysUserVO.setPhone(sysUser.getPhone());
        sysUserVO.setEmail(sysUser.getEmail());
        sysUserVO.setStatus(sysUser.getStatus());
        sysUserVO.setCreateTime(sysUser.getCreateTime());
        sysUserVO.setLastLoginIp(sysUser.getLastLoginIp());
        sysUserVO.setLastLoginTime(sysUser.getLastLoginTime());

        return sysUserVO;
    }

    public static QueryWrapper<SysUser> param2Wrapper(SysUserParam param) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .orderByAsc(SysUser::getCreateTime)
                .like(StrUtil.isNotBlank(param.getUsername()), SysUser::getUsername, param.getUsername())
                .like(StrUtil.isNotBlank(param.getNickname()), SysUser::getNickname, param.getNickname())
                .eq(StrUtil.isNotBlank(param.getStatus()), SysUser::getStatus, param.getStatus());
        return wrapper;
    }

    public static QueryWrapper<SysUser> username2Wrapper(String username) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(StrUtil.isNotBlank(username), SysUser::getUsername, username);
        return wrapper;
    }
}
